package com.abvnet.project.water.project.controller;

import com.abvnet.common.utils.StringUtils;
import com.abvnet.common.utils.file.FileUploadUtils;
import com.abvnet.common.utils.security.ShiroUtils;
import com.abvnet.framework.aspectj.lang.annotation.Log;
import com.abvnet.framework.aspectj.lang.enums.BusinessType;
import com.abvnet.framework.config.AbvnetConfig;
import com.abvnet.framework.web.controller.BaseController;
import com.abvnet.framework.web.domain.AjaxResult;
import com.abvnet.framework.web.page.TableDataInfo;
import com.abvnet.project.water.project.domain.ProjectInfo;
import com.abvnet.project.water.project.domain.ProjectStatus;
import com.abvnet.project.water.project.domain.ProjectTest;
import com.abvnet.project.water.project.service.IProjectInfoService;
import com.abvnet.project.water.project.service.IProjectTestService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/project/projectTest")
@Slf4j
public class ProjectTestController extends BaseController {

    private String prefix = "project/projectTest";

    @Autowired
    private IProjectInfoService projectInfoService;

    @Autowired
    private IProjectTestService projectTestService;

    @RequiresPermissions("project:projectTest:list")
    @GetMapping()
    public String projectDiagnosis()
    {
        return prefix + "/list";
    }

    /**
     * 查询项目小试列表
     */
    @RequiresPermissions("project:projectTest:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProjectInfo projectInfo) {
        if (!ShiroUtils.getSysUser().isAdmin()) { // 非管理员，只有数据可见中的用户能看到
            projectInfo.setCreateBy(ShiroUtils.getLoginName());
            projectInfo.setVisibleUserId(String.valueOf(ShiroUtils.getUserId()));
        }
        startPage();
        List<ProjectInfo> list = projectInfoService.selectProjectInfoList(projectInfo);
        return getDataTable(list);
    }

    /**
     * 修改项目小试
     */
    @GetMapping("/edit/{testId}")
    public String edit(@PathVariable("testId") Long testId, ModelMap mmap) {
        ProjectTest projectTest = projectTestService.selectProjectTestById(testId);
        mmap.put("projectTest", projectTest);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目小试
     */
    @RequiresPermissions("project:projectTest:edit")
    @Log(title = "项目小试", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProjectTest projectTest, @RequestParam("testFile") MultipartFile testFile) {
        try {
            if (testFile != null && !testFile.isEmpty()) {
                String testUrl = FileUploadUtils.upload(AbvnetConfig.getSmallPath(), testFile);
                projectTest.setTestUrl(testUrl);
            }
            ProjectInfo projectInfo = projectInfoService.selectProgressCodeById(projectTest.getProjectId());
//            if (StringUtils.equals(ProjectStatus.DIAG.getCode(),projectInfo.getProgressCode())) {
//                projectInfo.setProgressCode(ProjectStatus.TEST.getCode());
//                projectInfoService.updateProjectInfo(projectInfo);
//            }
            if (Integer.parseInt(projectInfo.getProgressCode()) < Integer.parseInt(ProjectStatus.TEST.getCode())) {
                projectInfo.setProgressCode(ProjectStatus.TEST.getCode());
                projectInfoService.updateProjectInfo(projectInfo);
            }
            return toAjax(projectTestService.updateProjectTest(projectTest));
        } catch (Exception e) {
            log.error("更新项目小试失败！", e);
            return error(e.getMessage());
        }
    }

    @RequiresPermissions("project:projectTest:list")
    @GetMapping("/detail/{projectId}")
    public String detail(@PathVariable("projectId") Long projectId, ModelMap mmap) {
        mmap.put("projectTest", projectTestService.selectProjectTestByProjectId(projectId));
        return prefix + "/detail";
    }
}